package io.web.utils;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;

import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @see SecureUtil
 * https://doc.hutool.cn/pages/Sign/#%E4%BB%8B%E7%BB%8D
 * <p>
 * byte[] data = "我是一段测试字符串".getBytes();
 * Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA);
 * //签名
 * byte[] signed = sign.sign(data);
 * //验证签名
 * boolean verify = sign.verify(data, signed);
 */
public class SignUtils {

    private static final Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA, privateHex(), publicHex());

    /**
     * 获取签名
     *
     * @param content
     * @return
     */
    public static String sign(String content, String secretKey) {
        return sign.signHex(saltString(content, secretKey).getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 验证签名
     *
     * @param content
     * @return
     */
    public static boolean verify(String content, String signStr, String secretKey) {
        byte[] signed = HexUtil.decodeHex(signStr);
        return sign.verify(saltString(content, secretKey).getBytes(StandardCharsets.UTF_8), signed);
    }

    private static String saltString(String content, String secretKey) {
        return content + "." + secretKey;
    }

    public static void main(String[] args) {
//        KeyPair pair = SecureUtil.generateKeyPair("RSA");
//        PrivateKey privateKey = pair.getPrivate();
//        PublicKey publicKey = pair.getPublic();
//        System.out.println(HexUtil.encodeHexStr(privateKey.getEncoded()));
//        System.out.println(HexUtil.encodeHexStr(publicKey.getEncoded()));
        String signStr = sign("abc", "123");
        boolean f = verify("abc", signStr, "123");
        System.out.println(f);
    }

    static String privateHex() {
        return "30820276020100300d06092a864886f70d0101010500048202603082025c02010002818100aa1fd8684418d79c77544c74e688539b14a6aba89e29044889f196ff6455d646b5d310685b5d2b557be3e84b59a0618a03f4ff33ab9979d23ca1ed29326c731645032b20930398cab233367a019be641e6e8d16851849ab33bc58d743687db883965ad57b34e80020a86275ed81bcc8c88d6b153bdae94a5deeca0df36980631020301000102818009ae10313696b745f6e03c0d76c4910544f28ad7e84feb277979de9e9f85e6d0552e3243cdd70a4d2de474e63cae4c7e7ee524d43ad25bc523a00f0f8607f7b057a336f0bba7f4f2697f2bbb887c17e683f5ce6dc9e7521b3a666900317f3c3525a7c578edef4353186618830a524fd00deaa654c996f6273b54eeede3647e01024100f56450d0e2563622673e535ff006efc45af72be6d8f9bf8c0a91ab5e888c56fbab6299028fe80537dedd36de9837a72289505b9df93b08aef34af4d681886d61024100b17a90b88c5040c97d4e760918d0de6bb4f9a6e814da7d9cd350123c80818b8674460167c88fca31c8b875f30bb132a37a4b1266e48755fdd22690095f26fad102404c26ef758367e1b0d77d5dfe5dcca947b43b1462b55efbf487ab21155b26c22fa3c61b59d232dcbbb3ebce855193a3c871f880160c2c27cbb330c5be73c054c102402bcc305caa9bbf7139d01c06e0ade2c5b7b28842ebaa83a728eab0a5679d107e8cc11de5abb3f854d2d0daa2b0f6f8a5292fcf89bec9bb29d92b950e0b024691024100ca44fd23548eda78a254610afea0b826691d94cb26b0877654ccaea7488c981bb4312efb198c222d487665af1d8c9b276b53ee58fac26ca01181aa9ce2903aaf";
    }

    static String publicHex() {
        return "30819f300d06092a864886f70d010101050003818d0030818902818100aa1fd8684418d79c77544c74e688539b14a6aba89e29044889f196ff6455d646b5d310685b5d2b557be3e84b59a0618a03f4ff33ab9979d23ca1ed29326c731645032b20930398cab233367a019be641e6e8d16851849ab33bc58d743687db883965ad57b34e80020a86275ed81bcc8c88d6b153bdae94a5deeca0df369806310203010001";
    }
}
